package com.qys.livesMall.event.listener;

import com.qys.livesMall.financial.model.entity.FinancialOfflineUser;
import com.qys.livesMall.financial.model.entity.FinancialOnlineDetail;
import com.qys.livesMall.financial.service.FinancialOfflineUserService;
import com.qys.livesMall.financial.service.FinancialOnlineDetailService;
import com.qys.livesMall.shop.event.OperateReportFormsEvent;
import com.qys.livesMall.shop.modal.entity.SysOperateReportForms;
import com.qys.livesMall.shop.service.SysOperateReportFormsService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class OperateReportFormsListener {
    @Resource
    private SysOperateReportFormsService sysOperateReportFormsService;
    @Resource
    private FinancialOfflineUserService financialOfflineUserService;
    @Resource
    private FinancialOnlineDetailService financialOnlineDetailService;
    @Order
    @EventListener(OperateReportFormsEvent.class)
    public void saveBalanceLog(OperateReportFormsEvent event) {
        SysOperateReportForms reportForms = (SysOperateReportForms) event.getSource();
        //线下理财
        List<FinancialOfflineUser> list = financialOfflineUserService.getYesterday();
        reportForms.setOfflineFinancingNum(CollectionUtils.isEmpty(list)?0: (int) list.stream().map(FinancialOfflineUser::getUserId).distinct().count());
        //线上理财
        List<FinancialOnlineDetail> financialOnlineDetails = financialOnlineDetailService.getYesterday();
        reportForms.setOnlineFinancingNum(CollectionUtils.isEmpty(financialOnlineDetails)?0: (int) financialOnlineDetails.stream().map(FinancialOnlineDetail::getUserId).distinct().count());
        sysOperateReportFormsService.save(reportForms);
    }
}
